package com.lukk.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lukk.constant.Const;
import com.lukk.dao.ProvinceDAO;
import com.lukk.dao.WorkerDAO;
import com.lukk.pojo.InternalWorker;
import com.lukk.pojo.Province;
import com.lukk.pojo.Territory;
import com.lukk.service.IProvinceService;

/**
 * 
 * ProvincesServiceImpl 下午2:57:03 Copyright zhaocj Inc. All Rights reserved.
 * alldios@outlook.com
 *
 */
@Service("provinceService")
public class ProvinceServiceImpl implements IProvinceService {
	private static Logger logger = LogManager.getLogger(ProvinceServiceImpl.class.getName());
	@Autowired
	private ProvinceDAO provinceDAO;

	public ProvinceDAO getProvinceDAO() {
		return provinceDAO;
	}

	public void setProvinceDAO(ProvinceDAO provinceDAO) {
		this.provinceDAO = provinceDAO;
	}

	@Override
	public List<Province> selectUnallocatedProvinces() {
		return provinceDAO.selectUnallocatedProvinces();
	}

	@Resource
	private WorkerDAO workerDAO;

	@Override
	public Map<String, List<Province>> selectAvailableProvinces(InternalWorker internalWorker) {
		Map<String, List<Province>> retsMap = new HashMap<String, List<Province>>();
		List<Province> provinces = provinceDAO.selectAvailableProvinces(internalWorker);
		List<Province> unallocatedProvinces = provinceDAO.selectUnallocatedProvincesByinternalWorker(internalWorker);
		retsMap.put("provinces", provinces);
		retsMap.put("unallocatedProvinces", unallocatedProvinces);
		return retsMap;
	}

	@Override
	public boolean modifyProvincesByInternalWorker(String provinceIDS, InternalWorker internalWorker) {
		if (!"".equals(provinceIDS)) {
			String[] split = provinceIDS.split(",");
			Integer[] splits = new Integer[split.length];
			for (int i = 0; i < split.length; i++) {
				splits[i] = Integer.valueOf(split[i]);
			}
			provinceDAO.modifyProvincesByInternalWorker(splits, internalWorker.getId());
		} else {
			// 若是没有选择 那就清空呗
			provinceDAO.modifyProvincesByInternalWorker(null, internalWorker.getId());
		}
		return true;
	}

	@Override
	public List<Province> selectAllocatedProvincesForInternalWorker(HttpSession session) {
		InternalWorker internalWorker = (InternalWorker) session.getAttribute(Const.SESSION_USER);
		return provinceDAO.selectAvailableProvinces(internalWorker);

	}

	@Override
	public List<Territory> selectCityByProvinceCode(String proviceCode) {
		return provinceDAO.selectCityByProvinceCode(proviceCode);
	}

	@Override
	public Map<String, Object> selectCountByCity(String cityCode) {
		List<Territory> terrs = provinceDAO.selectAreaByCity(cityCode);
		List<Integer> chartY = new ArrayList<Integer>();
		List<String> chartX = new ArrayList<String>();
		for (Territory territory : terrs) {
			chartX.add(territory.getCountry());
			chartY.add(provinceDAO.selectCountByCountry(territory.getCountry()));
		}
		Map<String, Object> rets = new HashMap<String, Object>();
		rets.put("chartX", chartX);
		rets.put("chartY", chartY);
		return rets;
	}

	@Override
	public Map<String, Object> selectTableCountByProvinceCode(String provinceCode) {
		Map<String, Object> retsMap = new HashMap<String, Object>();
		if ("0".equals(provinceCode)) {
			retsMap.put("total", 0);
			retsMap.put("rows", new ArrayList<>());
			return retsMap;
		}
		// 根据省份code查询省份信息
		Territory province = provinceDAO.selectProvinceByCode(provinceCode);
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("queryType", 0);
		queryMap.put("queryCode", province.getCode());
		Integer countByArea = workerDAO.selectWorkerCountByArea(queryMap);
		logger.debug("查询到{}省份注册人数为：{}", province.getProvince(), countByArea);
		List<Territory> territories = provinceDAO.selectCityByProvinceCode(provinceCode);
		logger.debug("查询出来的这个省份的城市有{}个", territories.size());
		// 组装省市字段 格式为 name-code-count
		for (Territory territory : territories) {
			Map<String, Object> queryCityMap = new HashMap<String, Object>();
			queryCityMap.put("queryType", 1);
			queryCityMap.put("queryCode", territory.getCode());
			territory.setCity(territory.getCity() + "-" + territory.getCode() + "-"
					+ workerDAO.selectWorkerCountByArea(queryCityMap));
			territory.setProvince(province.getProvince() + "-" + province.getCode() + "-" + countByArea);
		}
		retsMap.put("total", territories.size());
		retsMap.put("rows", territories);
		return retsMap;
	}

	@Override
	public List<Territory> selectCountByCityCode(String cityCode) {
		List<Territory> territories = provinceDAO.selectAreaByCity(cityCode);
		for (Territory territory : territories) {
			logger.debug("查出的名字：{},code:{}", territory.getCountry(), territory.getCode());
			territory.setCount(provinceDAO.selectCountByCountryCode(territory.getCode()));
		}
		return territories;
	}

	@Override
	public List<Province> selectProvinces() {
		return provinceDAO.selectProvinces();
	}

	@Override
	public List<Province> selectAllocatedProvincesByIWorker(Integer iWorkerID) {
		InternalWorker internalWorker = new InternalWorker();
		internalWorker.setId(iWorkerID);
		return provinceDAO.selectAvailableProvinces(internalWorker);
	}

}
